package com.zyh.kafka.util;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.Arrays;
import java.util.Properties;

public class KafkaConsumerUtils {
    // 配置消费者参数
    private static Properties consumerConfig = new Properties();

    /**
     *
     * @param group 传入组名
     * @return 返回消费者对象
     */
    public static KafkaConsumer<String,String> get(String group){
        consumerConfig.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"kafka24:9092");
        consumerConfig.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer .class.getName());
        consumerConfig.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        consumerConfig.setProperty(ConsumerConfig.GROUP_ID_CONFIG,group);
        return new KafkaConsumer<>(consumerConfig);
    }

    /**
     * 释放资源
     * @param consumer 消费者对象
     */
    public static void close(KafkaConsumer<String,String> consumer){
        consumer.close();
    }

}
